﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Library
{
    public class StopwatchExt
    {
        /// <summary>
        /// 计时器
        /// </summary>
        Stopwatch _stopwatch;

        #region 构造函数
        public StopwatchExt()
        {
            _stopwatch = new Stopwatch();
        }

        public StopwatchExt(bool flag) : this()
        {
            if (flag)
            {
                _stopwatch.Start();
            }
        }
        #endregion

        #region 计时器操作
        /// <summary>
        /// 开始当前计时器
        /// </summary>
        public void Start()
        {
            _stopwatch.Start();
        }

        /// <summary>
        /// 停止当前计时器
        /// </summary>
        public void Stop()
        {
            _stopwatch.Stop();
        }
        #endregion

        #region 获取当前计时器
        /// <summary>
        /// 获取当前计时器
        /// </summary>
        /// <returns></returns>
        public Stopwatch GetStopwatch()
        {
            return _stopwatch;
        }
        #endregion

        #region 计时器执行时间-毫秒
        /// <summary>
        /// 计时器执行时间-毫秒
        /// </summary>
        /// <returns></returns>
        public long GetStopwatchMilliseconds()
        {
            TimeSpan time = _stopwatch.Elapsed;
            return (long)time.TotalMilliseconds;
        }

        #endregion

    }
}
